<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link href="jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css"
          rel="stylesheet"/>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript"
            src="jquery/bootstrap-datetimepicker-master/locale/bootstrap-datetimepicker.zh-CN.js"></script>
    <script type="text/javascript" src="jquery/bs_typeahead/bootstrap3-typeahead.min.js"></script>
    <script type="text/javascript">
        $(function () {

            $(".mydate").datetimepicker({
                language: 'zh-CN', //语言
                format: 'yyyy-mm-dd',//日期的格式
                minView: 'month', //可以选择的最小视图
                initialDate: new Date(),//初始化显示的日期
                autoclose: true,//设置选择完日期或者时间之后，日否自动关闭日历
                todayBtn: true,//设置是否显示"今天"按钮,默认是false
                clearBtn: true//设置是否显示"清空"按钮，默认是false
            });

            //给"阶段"下拉框添加change事件
            $("#update-stage").change(function () {
                //收集参数
                //alert($(this).find("option:selected").text());
                var stageValue = $("#update-stage option:selected").text();
                //表单验证
                if (stageValue == "") {
                    //清空可能性输入框
                    $("#update-possibility").val("");
                    return;
                }
                //发送请求
                $.ajax({
                    url: 'workbench/transaction/getPossibilityByStage',
                    data: {
                        stageValue: stageValue
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //把可能性显示在输入框中
                        $("#update-possibility").val(data);
                    }
                });
            });

            //当容器加载完成之后，对容器调用工具函数
            $("#update-customerName").typeahead({
                source: function (jquery, process) {//每次键盘弹起，都自动触发本函数；我们可以向后台送请求，查询客户表中所有的名称，把客户名称以[]字符串形式返回前台，赋值给source
                    //process：是个函数，能够将['xxx','xxxxx','xxxxxx',.....]字符串赋值给source，从而完成自动补全
                    //jquery：在容器中输入的关键字
                    //var customerName=$("#customerName").val();
                    //发送查询请求
                    $.ajax({
                        url: 'workbench/transaction/queryCustomerNamesLikeName',
                        data: {
                            customerName: jquery
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {//['xxx','xxxxx','xxxxxx',.....]
                            process(data);
                        }
                    });
                }
            });

            //给"创建-市场活动源"搜索按钮添加单击事件
            $("#searchActivityBtn").click(function () {
                $("#searchActivityTxt").val("");
                //清空搜索列表
                $("#activityTBody").html("");

                //弹出搜索市场活动的模态窗口
                $("#searchActivityModal").modal("show");
            });

            //给"创建-市场活动源"搜索框添加键盘弹起事件
            $("#searchActivityTxt").keyup(function () {
                //收集参数
                var activityName = this.value;
                //发送请求
                $.ajax({
                    url: 'workbench/activity/queryActivityLikeName',
                    data: {
                        activityName: activityName
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //遍历data,显示所有搜索的市场活动
                        var htmlStr = "";
                        $.each(data, function (index, obj) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"radio\" value=\"" + obj.id + "\" activityName=\"" + obj.name + "\" name=\"activity\"/></td>";
                            htmlStr += "<td>" + obj.name + "</td>";
                            htmlStr += "<td>" + obj.startDate + "</td>";
                            htmlStr += "<td>" + obj.endDate + "</td>";
                            htmlStr += "<td>" + obj.owner + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#activityTBody").html(htmlStr);
                    }
                });
            });

            //给"创建-市场活动源"的单选按钮添加单击事件
            $("#activityTBody").on("click", "input[type='radio']", function () {
                //获取市场活动的id和name
                var id = this.value;
                var activityName = $(this).attr("activityName");
                //把市场活动的id写到隐藏域，把name写到输入框中
                $("#create-activityId").val(id);
                $("#create-activityName").val(activityName);
                //关闭搜索市场活动的模态窗口
                $("#searchActivityModal").modal("hide");
            });

            //给"创建-联系人名称"搜索按钮添加单击事件
            $("#searchContactsBtn").click(function () {
                $("#searchContactsTxt").val("");
                //清空搜索列表
                $("#contactsTBody").html("");

                //弹出搜索联系人的模态窗口
                $("#searchContactsModal").modal("show");
            });

            //给"创建-联系人名称"搜索框添加键盘弹起事件
            $("#searchContactsTxt").keyup(function () {
                //收集参数
                var contactsName = this.value;
                //发送请求
                $.ajax({
                    url: 'workbench/contacts/queryContactsLikeName',
                    data: {
                        contactsName: contactsName
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //遍历data,显示所有搜索的市场活动
                        var htmlStr = "";
                        $.each(data, function (index, obj) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"radio\" value=\"" + obj.id + "\" contactsName=\"" + obj.fullname + "\" name=\"contacts\"/></td>";
                            htmlStr += "<td>" + obj.fullname + "</td>";
                            htmlStr += "<td>" + obj.email + "</td>";
                            htmlStr += "<td>" + obj.mphone + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#contactsTBody").html(htmlStr);
                    }
                });
            });

            //给"创建-联系人名称"的单选按钮添加单击事件
            $("#contactsTBody").on("click", "input[type='radio']", function () {
                //获取市场活动的id和name
                var id = this.value;
                var contactsName = $(this).attr("contactsName");
                //把市场活动的id写到隐藏域，把name写到输入框中
                $("#create-contactsId").val(id);
                $("#create-contactsName").val(contactsName);
                //关闭搜索市场活动的模态窗口
                $("#searchContactsModal").modal("hide");
            });

            //给"更新"按钮添加单击事件
            $("#updateTranBtn").click(function () {
                //收集参数
                var id = $("#edit-id").val();
                var owner = $("#update-transactionOwner").val();
                var money = $.trim($("#update-amountOfMoney").val());
                var name = $("#update-transactionName").val();
                var expectedDate = $("#update-expectedClosingDate").val();
                var customerName = $("#update-customerName").val();
                var customerId = $("#update-customerId").val();
                var type = $.trim($("#update-transactionType").val());
                var stage = $.trim($("#update-transactionStage").val());
                var source = $.trim($("#update-clueSource").val());
                var activityId = $.trim($("#create-activityId").val());
                var contactsId = $.trim($("#create-contactsId").val());
                var description = $.trim($("#update-describe").val());
                var contactSummary = $.trim($("#update-contactSummary").val());
                var nextContactTime = $.trim($("#update-nextContactTime").val());

                if (owner == "") {
                    alert("所有者不能为空");
                    return;
                }
                if (name == "") {
                    alert("姓名不能为空");
                    return;
                }
                if (expectedDate == "") {
                    alert("预计成交日期不能为空");
                    return;
                }
                if (customerName == "") {
                    alert("客户名称不能为空");
                    return;
                }
                if (stage == "") {
                    alert("阶段不能为空");
                    return;
                }
                if (type == "") {
                    alert("类型不能为空");
                    return;
                }
                if (source == "") {
                    alert("来源不能为空");
                    return;
                }
                if (activityId == "") {
                    alert("市场活动不能为空");
                    return;
                }
                if (contactsId == "") {
                    alert("联系人不能为空");
                    return;
                }
                var regExp = /^(([1-9]\d*)|0)$/;
                if (!regExp.test(money)) {
                    alert("金额只能为非负整数");
                    return;
                }

                $.ajax({
                    url: 'workbench/transaction/update',
                    data: {
                        id: id,
                        owner: owner,
                        money: money,
                        name: name,
                        customerId: customerId,
                        expectedDate: expectedDate,
                        stage: stage,
                        type: type,
                        source: source,
                        activityId: activityId,
                        contactsId: contactsId,
                        description: description,
                        contactSummary: contactSummary,
                        nextContactTime: nextContactTime
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == "1") {
                            //关闭模态窗口
                            $("#updateTransactionModal").modal("hide");
                            window.location.href = "/workbench/transaction/index"
                        } else {
                            //提示信息
                            alert(data.message);
                            //模态窗口不关闭
                            $("#updateTransactionModal").modal("show");
                        }
                    }
                });
            });
        });
    </script>
</head>

<body>
<!-- 查找市场活动模态窗口 -->
<div class="modal fade" id="searchActivityModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">查找市场活动</h4>
            </div>
            <div class="modal-body">
                <div class="btn-group" style="position: relative; top: 18%; left: 8px;">
                    <form class="form-inline" role="form">
                        <div class="form-group has-feedback">
                            <input id="searchActivityTxt" type="text" class="form-control" style="width: 300px;"
                                   placeholder="请输入市场活动名称，支持模糊查询">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                    </form>
                </div>
                <table id="activityTable3" class="table table-hover"
                       style="width: 900px; position: relative;top: 10px;">
                    <thead>
                    <tr style="color: #B3B3B3;">
                        <td></td>
                        <td>名称</td>
                        <td>开始日期</td>
                        <td>结束日期</td>
                        <td>所有者</td>
                    </tr>
                    </thead>
                    <tbody id="activityTBody">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 查找联系人模态窗口 -->
<div class="modal fade" id="searchContactsModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">查找联系人</h4>
            </div>
            <div class="modal-body">
                <div class="btn-group" style="position: relative; top: 18%; left: 8px;">
                    <form class="form-inline" role="form">
                        <div class="form-group has-feedback">
                            <input type="text" class="form-control" id="searchContactsTxt" style="width: 300px;"
                                   placeholder="请输入联系人名称，支持模糊查询">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                    </form>
                </div>
                <table id="activityTable" class="table table-hover" style="width: 900px; position: relative;top: 10px;">
                    <thead>
                    <tr style="color: #B3B3B3;">
                        <td></td>
                        <td>名称</td>
                        <td>邮箱</td>
                        <td>手机</td>
                    </tr>
                    </thead>
                    <tbody id="contactsTBody">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div style="position:  relative; left: 30px;">
    <h3>修改交易</h3>
    <div style="position: relative; top: -40px; left: 70%;">
        <button type="button" class="btn btn-primary" id="updateTranBtn">更新</button>
        <button type="button" class="btn btn-default">取消</button>
    </div>
    <hr style="position: relative; top: -40px;">
</div>

<form class="form-horizontal" role="form" style="position: relative; top: -30px;">
    <input type="hidden" id="edit-id" value="${transaction.id}">
    <div class="form-group">
        <label for="update-transactionOwner" class="col-sm-2 control-label">所有者<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="update-transactionOwner">
                <c:forEach items="${userList}" var="u">
                    <option value="${u.id}">${u.name}</option>
                </c:forEach>
            </select>
        </div>
        <label for="update-amountOfMoney" class="col-sm-2 control-label">金额</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="update-amountOfMoney" value="${transaction.money}">
        </div>
    </div>

    <div class="form-group">
        <label for="update-transactionName" class="col-sm-2 control-label">名称<span style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="update-transactionName" value="${transaction.name}">
        </div>
        <label for="update-expectedClosingDate" class="col-sm-2 control-label">预计成交日期<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control mydate" name="mydate" readonly id="update-expectedClosingDate" value="${transaction.expectedDate}">
        </div>
    </div>

    <div class="form-group">
        <label for="update-accountName" class="col-sm-2 control-label">客户名称<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="hidden" id="update-customerId" value="${transaction.customerId}">
            <input type="text" class="form-control" id="update-accountName" placeholder="支持自动补全，输入客户不存在则新建"
                   value="${transaction.customerName}">
        </div>
        <label for="update-transactionStage" class="col-sm-2 control-label">阶段<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="update-transactionStage">
                <c:forEach items="${stageList}" var="s">
                    <option value="${s.id}">${s.value}</option>
                </c:forEach>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label for="update-transactionType" class="col-sm-2 control-label">类型</label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="update-transactionType">
                <c:forEach items="${transactionTypeList}" var="tt">
                    <option value="${tt.id}">${tt.value}</option>
                </c:forEach>
            </select>
        </div>
        <label for="update-possibility" class="col-sm-2 control-label">可能性</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="update-possibility" readonly>
        </div>
    </div>

    <div class="form-group">
        <label for="update-clueSource" class="col-sm-2 control-label">来源</label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="update-clueSource">
                <c:forEach items="${sourceList}" var="so">
                    <option value="${so.id}">${so.value}</option>
                </c:forEach>
            </select>
        </div>
        <label for="create-activityId" class="col-sm-2 control-label">市场活动源&nbsp;&nbsp;<a
                href="javascript:void(0);"
                id="searchActivityBtn"
                data-toggle="modal">
            <span class="glyphicon glyphicon-search"></span></a></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="hidden" id="create-activityId" value="${transaction.activityId}">
            <input type="text" class="form-control" id="create-activityName" value="${transaction.activityName}" readonly>
        </div>
    </div>


    <div class="form-group">
        <label for="create-contactsId" class="col-sm-2 control-label">联系人名称&nbsp;&nbsp;
            <a
                    href="javascript:void(0);"
                    data-toggle="modal"
                    id="searchContactsBtn">
                <span class="glyphicon glyphicon-search"></span>
            </a>
        </label>

        <div class="col-sm-10" style="width: 300px;">
            <input type="hidden" id="create-contactsId" value="${transaction.contactsId}">
            <input type="text" class="form-control" id="create-contactsName" value="${transaction.contactsName}" readonly>
        </div>
    </div>


    <div class=" form-group">
        <label for="update-describe" class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10" style="width: 70%;">
            <textarea class="form-control" rows="3" id="update-describe" >${transaction.description}</textarea>
        </div>
    </div>

    <div class="form-group">
        <label for="update-contactSummary" class="col-sm-2 control-label">联系纪要</label>
        <div class="col-sm-10" style="width: 70%;">
            <textarea class="form-control" rows="3" id="update-contactSummary">${transaction.contactSummary}</textarea>
        </div>
    </div>

    <div class=" form-group">
        <label for="update-nextContactTime" class="col-sm-2 control-label">下次联系时间</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control mydate" name="mydate" readonly id="update-nextContactTime" value="${transaction.nextContactTime}">
        </div>
    </div>
</form>
</body>
</html>